#pragma once

#include "MP_FileIO.h"

using namespace std;

int DataFile::ReadUavData(string file, UAV_STATE uavs[MAX_UAV_NUM])
{
	ifstream uavFile;
	uavFile.open(file);
	if (uavFile.is_open())
	{
		string lineStr;
		UAV_STATE lineUav;
		int numUav = 0;

		char tempChar0[1024]; char tempChar1[1024]; char tempChar2[1024]; char tempChar3[1024];
		char tempChar4[1024]; char tempChar5[1024]; char tempChar6[1024]; char tempChar7[1024];
		char tempChar8[1024]; char tempChar9[1024]; char tempChar10[1024]; char tempChar11[1024];

		while (getline(uavFile, lineStr))
		{
			if (lineStr[0] != '@')
				continue;
			memset(tempChar0, 0, sizeof(tempChar0)); memset(tempChar1, 0, sizeof(tempChar1));
			memset(tempChar2, 0, sizeof(tempChar2)); memset(tempChar3, 0, sizeof(tempChar3));
			memset(tempChar4, 0, sizeof(tempChar4)); memset(tempChar5, 0, sizeof(tempChar5));
			memset(tempChar6, 0, sizeof(tempChar6)); memset(tempChar7, 0, sizeof(tempChar7));
			memset(tempChar8, 0, sizeof(tempChar8)); memset(tempChar9, 0, sizeof(tempChar9));
			memset(tempChar10, 0, sizeof(tempChar10)); memset(tempChar11, 0, sizeof(tempChar11));

			sscanf_s(lineStr.c_str(), "%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s\n", tempChar0, \
				sizeof(tempChar0), tempChar1, sizeof(tempChar1), tempChar2, sizeof(tempChar2), tempChar3, \
				sizeof(tempChar3), tempChar4, sizeof(tempChar4), tempChar5, sizeof(tempChar5), tempChar6, \
				sizeof(tempChar6), tempChar7, sizeof(tempChar7), tempChar8, sizeof(tempChar8), tempChar9, \
				sizeof(tempChar9), tempChar10, sizeof(tempChar10), tempChar11, sizeof(tempChar11));
			sscanf(tempChar1, "%lf", &lineUav.dLon);
			sscanf(tempChar2, "%lf", &lineUav.dLat);
			sscanf(tempChar3, "%f", &lineUav.fHeight);
			sscanf(tempChar4, "%f,%f,%f", &lineUav.fRoll, &lineUav.fPitch, &lineUav.fYaw);
			sscanf(tempChar5, "%f", &lineUav.fVelocity);
			sscanf(tempChar6, "%f", &lineUav.fRadius);
			//lineUav.fAngle = -lineUav.fYaw;
			//lineUav.nType = 0;
			lineUav.fDegDamage = 0;
			uavs[numUav] = lineUav;
			numUav++;
		}
		uavFile.close();
		return numUav;
	}
	else
	{
		uavFile.close();
		return 0;
	}
}

int DataFile::ReadTargetData(string file, TARGET_STATE targets[MAX_TARGET_NUM])
{
	ifstream targetFile;
	targetFile.open(file);
	if (targetFile.is_open())
	{
		string lineStr;
		TARGET_STATE lineTarget;
		int numTarget = 0;
		char tempChar0[1024]; char tempChar1[1024]; char tempChar2[1024];
		char tempChar3[1024]; char tempChar4[1024]; char tempChar5[1024];
		char tempChar6[1024]; char tempChar7[1024]; char tempChar8[1024];
		char tempChar9[1024]; char tempChar10[1024]; char tempChar11[1024];
		char tempChar12[1024];

		while (getline(targetFile, lineStr))
		{
			if (lineStr[0] != '@')
				continue;
			memset(tempChar0, 0, sizeof(tempChar0)); memset(tempChar1, 0, sizeof(tempChar1));
			memset(tempChar2, 0, sizeof(tempChar2)); memset(tempChar3, 0, sizeof(tempChar3));
			memset(tempChar4, 0, sizeof(tempChar4)); memset(tempChar5, 0, sizeof(tempChar5));
			memset(tempChar6, 0, sizeof(tempChar6)); memset(tempChar7, 0, sizeof(tempChar7));
			memset(tempChar8, 0, sizeof(tempChar8)); memset(tempChar9, 0, sizeof(tempChar9));
			memset(tempChar10, 0, sizeof(tempChar10)); memset(tempChar11, 0, sizeof(tempChar11));
			memset(tempChar12, 0, sizeof(tempChar12));
			sscanf_s(lineStr.c_str(), "%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s\n", tempChar0, \
				sizeof(tempChar0), tempChar1, sizeof(tempChar1), tempChar2, sizeof(tempChar2), tempChar3, \
				sizeof(tempChar3), tempChar4, sizeof(tempChar4), tempChar5, sizeof(tempChar5), tempChar6, \
				sizeof(tempChar6), tempChar7, sizeof(tempChar7), tempChar8, sizeof(tempChar8), tempChar9, \
				sizeof(tempChar9), tempChar10, sizeof(tempChar10), tempChar11, sizeof(tempChar11), \
				tempChar12, sizeof(tempChar12));
			//sscanf(tempChar1, "%d", &lineTarget.nType);
			//lineTarget.nType = lineTarget.nType - 1;
			sscanf(tempChar2, "%lf", &lineTarget.dLon);
			sscanf(tempChar3, "%lf", &lineTarget.dLat);
			sscanf(tempChar4, "%f", &lineTarget.fHeight);
			sscanf(tempChar5, "%f", &lineTarget.fAttackAngle);
			sscanf(tempChar6, "%f", &lineTarget.fAttackAngleMin);
			sscanf(tempChar7, "%f", &lineTarget.fAttackAngleMax);
			sscanf(tempChar8, "%d", &lineTarget.nNeedUavNum);
			sscanf(tempChar9, "%f", &lineTarget.fDegImportance);
			sscanf(tempChar10, "%f", &lineTarget.fDegThreat);
			targets[numTarget] = lineTarget;
			numTarget++;
		}

		targetFile.close();
		return numTarget;
	}
	else
	{
		targetFile.close();
		return 0;
	}
}

int DataFile::ReadForbidAreaData(string file, FORBID_AREA_POINT& area)
{
	ifstream obsFile;
	obsFile.open(file);
	FORBID_AREA_POINT obsPoints[MAX_OBS_POINT_NUM];
	int numPoint = 0;
	if (obsFile.is_open())
	{
		string lineStr;
		FORBID_AREA_POINT linePoint;

		char tempChar0[1024]; char tempChar1[1024]; char tempChar2[1024]; char tempChar3[1024]; char tempChar4[1024];
		char tempChar5[1024]; char tempChar6[1024]; char tempChar7[1024]; char tempChar8[1024]; char tempChar9[1024];
		char tempChar10[1024];

		while (getline(obsFile, lineStr))
		{
			if (lineStr[0] != '@')
				continue;
			memset(tempChar0, 0, sizeof(tempChar0)); memset(tempChar1, 0, sizeof(tempChar1));
			memset(tempChar2, 0, sizeof(tempChar2)); memset(tempChar3, 0, sizeof(tempChar3));
			memset(tempChar4, 0, sizeof(tempChar4)); memset(tempChar5, 0, sizeof(tempChar5));
			memset(tempChar6, 0, sizeof(tempChar6)); memset(tempChar7, 0, sizeof(tempChar7));
			memset(tempChar8, 0, sizeof(tempChar8)); memset(tempChar9, 0, sizeof(tempChar9));
			memset(tempChar10, 0, sizeof(tempChar10));
			sscanf_s(lineStr.c_str(), "%s	%s	%s	%s	%s	%s	%s	%s	%s	%s	%s\n",
				tempChar0, sizeof(tempChar0), tempChar1, sizeof(tempChar1),
				tempChar2, sizeof(tempChar2), tempChar3, sizeof(tempChar3),
				tempChar4, sizeof(tempChar4), tempChar5, sizeof(tempChar5),
				tempChar6, sizeof(tempChar6), tempChar7, sizeof(tempChar7),
				tempChar8, sizeof(tempChar8), tempChar9, sizeof(tempChar9),
				tempChar10, sizeof(tempChar10));

			sscanf(tempChar1, "%d", &linePoint.nAreaID);
			sscanf(tempChar2, "%d", &linePoint.nAreaType);
			sscanf(tempChar3, "%lf", &linePoint.sCenterPoint.dLon);
			sscanf(tempChar4, "%lf", &linePoint.sCenterPoint.dLat);
			sscanf(tempChar5, "%f", &linePoint.fHeight);
			sscanf(tempChar6, "%f", &linePoint.fRadius);
			sscanf(tempChar7, "%f", &linePoint.fStartAngle);
			sscanf(tempChar8, "%f", &linePoint.fFinishAngle);

			obsPoints[numPoint] = linePoint;
			numPoint++;
		}
		obsFile.close();
	}
	else
	{
		obsFile.close();
		return 0;
	}

	GEO_POINT PolygonPoints[32][10];
	int PolygonsPointNum[32] = { 0 };
	bool PolygonPointsExistState[32][10] = { false };
	for (int i_polygon = 0; i_polygon < 32; i_polygon++)
	{
		PolygonsPointNum[i_polygon] = 0;
		for (int i_point = 0; i_point < 10; i_point++)
		{
			PolygonPointsExistState[i_polygon][i_point] = false;
			PolygonPoints[i_polygon][i_point].dLon = 0;
			PolygonPoints[i_polygon][i_point].dLat = 0;
		}
	}

	for (int i_obspoint = 0; i_obspoint < numPoint; i_obspoint++)
	{
		if (obsPoints[i_obspoint].nAreaType != 1)
			continue;

		int PolygonID = obsPoints[i_obspoint].nAreaID;
		if ((PolygonID < 0) || (PolygonID > 32))
		{
			continue;
		}
		PolygonPointsExistState[PolygonID][PolygonsPointNum[PolygonID]] = true;
		PolygonPoints[PolygonID][PolygonsPointNum[PolygonID]].dLon = obsPoints[i_obspoint].sCenterPoint.dLon;
		PolygonPoints[PolygonID][PolygonsPointNum[PolygonID]].dLat = obsPoints[i_obspoint].sCenterPoint.dLat;
		PolygonPoints[PolygonID][PolygonsPointNum[PolygonID]].fHeight = obsPoints[i_obspoint].fHeight;
		PolygonsPointNum[PolygonID]++;
	}

	area.nAreaNum = 0;
	for (int i_polygon = 0; i_polygon < 32; i_polygon++)
	{
		if (PolygonsPointNum[i_polygon] == 0)
		{
			continue;
		}
		area.sArea[area.nAreaNum].nAreaType = 0;
		area.sArea[area.nAreaNum].nPointNum = PolygonsPointNum[i_polygon];
		area.sArea[area.nAreaNum].fHeight = 0.0;
		for (int i_point = 0; i_point < PolygonsPointNum[i_polygon]; i_point++)
		{
			area.sArea[area.nAreaNum].sPoint[i_point] = PolygonPoints[i_polygon][i_point];
			if (PolygonPoints[i_polygon][i_point].fHeight > area.sArea[area.nAreaNum].fHeight)
				area.sArea[area.nAreaNum].fHeight = PolygonPoints[i_polygon][i_point].fHeight;
		}
		area.nAreaNum++;
	}

	for (int i_obspoint = 0; i_obspoint < 256; i_obspoint++)
	{
		if (obsPoints[i_obspoint].nAreaType != 2)
			continue;
		
		area.sArea[area.nAreaNum].nAreaType = 2;
		area.sArea[area.nAreaNum].sCenterPoint.dLon = obsPoints[i_obspoint].sCenterPoint.dLon;
		area.sArea[area.nAreaNum].sCenterPoint.dLat = obsPoints[i_obspoint].sCenterPoint.dLat;
		area.sArea[area.nAreaNum].fHeight = obsPoints[i_obspoint].fHeight;
		area.sArea[area.nAreaNum].fRadius = obsPoints[i_obspoint].fRadius;
		area.sArea[area.nAreaNum].fStartAngle = obsPoints[i_obspoint].fStartAngle / 180.0 * M_PI;
		area.sArea[area.nAreaNum].fFinishAngle = obsPoints[i_obspoint].fFinishAngle / 180.0 * M_PI;
		area.nAreaNum++;
	}
	return 1;
}

int DataFile::WriteFlightLineData(string file, FLIGHT_LINE line)
{
	ofstream flightLineFile(file);
	for (int i_point = 0; i_point < line.nPointNum; i_point++)
	{
		char linePointStr[1024];
		sprintf(linePointStr, "@%d	%lf	%lf	%f	%f	%f	%f", i_point + 1,
			line.sPoint[i_point].dLon, line.sPoint[i_point].dLat, line.sPoint[i_point].fHeight,
			line.sPoint[i_point].fVelocity, line.sPoint[i_point].fLength, line.sPoint[i_point].fCurvature);
		flightLineFile << string(linePointStr) << endl;
	}
	flightLineFile.close();
	return 1;
}


